<template>
  <!-- 导航栏 -->
  <van-nav-bar
    title="编辑收货地址"
    left-text="返回"
    left-arrow
    @click-left="back"
  />

  <van-address-edit
    :area-list="areaList"
    :address-info="address"
    show-set-default
    show-delete
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
    @delete="onDel"
  />


  

</template>

<script setup>
  // 引入地区数据
  import { areaList } from '@vant/area-data';

  const {proxy} = getCurrentInstance()

  let business = proxy.$cookies.get('business') ? proxy.$cookies.get('business') : {}
  business = reactive(business)
  let id = proxy.$route.query.hasOwnProperty('id') ? proxy.$route.query.id : 0;
  id = ref(id)
  
  var area = business.region_text.split('/')
  var region = business.district || business.city || business.province

  let address = ref({
    name: business.nickname,
    tel: business.mobile,
    province: area[0],
    city:  area[1],
    county:  area[2],
    areaCode: region
  })
  
  const back = () => proxy.$router.go(-1)

  const onSave = async (values) =>
  {
    //组装数据
    var data = {
      id: id.value,
      busid: business.id,
      consignee: values.name,
      address: values.addressDetail,
      mobile: values.tel,
      status: values.isDefault ? '1' : '0',
      code: values.areaCode
    }

    var result = await proxy.$POST({
      url: '/address/edit',
      params: data
    })

    if(result.code == 0)
    {
      proxy.$fail(result.msg)
      return false
    }else
    {
      proxy.$success({
        message: result.msg,
        onClose: () =>
        {
          proxy.$router.go(-1)
        }
      })
    }
  }

  let onDel = async (info) => {
      //组装数据
      var data = {
          id: id.value,
          busid: business.id
      }

      //提醒
      proxy.$confirm({
          title: '删除提醒',
          message: '是否确认删除该收货地址',
      }).then(async () => {
          //发请求
          var result = await proxy.$POST({
              url: '/address/del',
              params: data
          })

          if(result.code == '0')
          {
              proxy.$fail(result.msg)
              return false
          }

          proxy.$success(result.msg)
          proxy.$router.go(-1)
      }).catch(() => {})
  }

  //挂载前生命周期
  onBeforeMount(() => {
    request()
  })

  const request = async () =>
  {

    var result = await proxy.$POST({
      url: '/address/info',
      params: {
        id: id.value,
        busid: business.id
      }
    })

    if(result.code == '0')
    {
      proxy.$fail({
        message: result.msg,
        duration: 1000,
        onClose: () => 
        {
          proxy.$router.go(-1)
        }
      })
    }

    var data = result.data 

    var code = data.district || data.city || data.province

    //重新赋值默认地址信息
    address.value = {
      name: data.consignee,
      tel: data.mobile,
      areaCode: code,
      addressDetail: data.address,
      isDefault: data.status == '1' ? true : false,
    }
  }

</script>


